#include "historylist.h"
#include "ui_historylist.h"
#include "itemform.h"
#include "databasemanager.h"
#include <QSqlQuery>
#include <QSqlDatabase>
#include <QDate>

historylist::historylist(QWidget *parent)
    : QWidget(parent)
    , ui(new Ui::historylist)
{
    ui->setupUi(this);
    ui->tab_2->setStyleSheet(
        "QTabWidget {"
        "background-color: transparent;"
        "}"
        );
}

historylist::~historylist()
{
    delete ui;
}

//所有的数据表
void historylist::sethistorylist()
{
    QSqlQuery query(DatabaseManager::getDatabase());

    QString sqlselect = "SELECT * FROM history WHERE user = ?;";
    query.prepare(sqlselect);
    query.addBindValue(DatabaseManager::getUID());
    //遍历数据库记录
    int length_history=0;
    int focus_history=0;
    ui->listWidget->clear();//清空上一次的
    query.exec();
    while(query.next()){
        int id=query.value("id").toInt();
        QString task_name=query.value("task_name").toString();
        QString task_type=query.value("task_type").toString();
        int duration=query.value("duration").toInt();
        QString date=query.value("date").toString();
        length_history+=duration;focus_history++;

        type_name_Data.append(qMakePair(id,task_name));
        duration_Data.append(qMakePair(id,duration));

        //每遍历一条更新ui组件setItemInfo
        itemform *historyitem=new itemform;
        historyitem->setItemInfo(task_name,task_type,duration,date);
        QListWidgetItem*item=new QListWidgetItem;

        item->setSizeHint(QSize(550,  80));

        //将每行与数据表连接
        ui->listWidget->addItem(item);
        ui->listWidget->setItemWidget(item,historyitem);
    }
    QString length_historyStr=QString::number(length_history);
    QString focus_historyStr=QString::number(focus_history);
    ui->label_9->setText(length_historyStr);
    ui->label_9->setAlignment(Qt::AlignVCenter);
    ui->label_6->setText(focus_historyStr);
    ui->label_6->setAlignment(Qt::AlignVCenter);
}

